/*
 * @Title: 姿美汇H5商城登录
 * @Author: JSL
 * @Date:   2018-10-13
 * @Last Modified by:  JSL
 * @Last Modified time: 2018-12-04 16:15:41
 */
var dataInfo = new Object(),
  template = {
    init: function() {
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          getCodeInfo: '获取验证码',
          warringInfo: '',
          phone: '',
          code: '',
          isWx: is_weixn(),
          codeNum: 1,
          switchNum: 1,
          statusSwitch: 1,
          isYY: 1,
        },
        methods: {
          switchAdd: function() { //是微信走登录 不是走填写手机号
            !!this.isWx ? this.wxLogin() : this.switchNum = 2;
          },
          loginClose: function() { //关闭填写手机号录入页面
            this.switchNum = 1;
          },
          submit: function() { //H5分销登录 - 填写手机号
            if (!!this.active && this.statusSwitch == 1) { //符合点击条件且只点击了一次
              this.statusSwitch = 2;
              ajaxTemplate(addressCodeUrl + SERVICE.fxH5Login, function(data) {
                if (data.tag == 0) {
                  dataInfo.submitData = data.data.result.data;
                  !!dataInfo.init.isWx ? dataInfo.submitData.openId = getQueryString('openid') : '';
                  setCookie('zmtH5Mall', escape(JSON.stringify(dataInfo.submitData)), 30);
                  dataInfo.init.detectionJump();
                } else {
                  dataInfo.init.statusSwitch = 1;
                  dataInfo.init.warringInfo = data.errMsg;
                };
              }, {
                phone: dataInfo.init.phone,
                verifyCode: dataInfo.init.code,
                type: 1,
                openId: !openId ? getQueryString('openid') : openId,
                shopCode: shareShopCode
              });
            };
          },
          getYYCode: function() { //获取语音验证码
            this.warringInfo = '';
            if (this.codeNum == 2) {
              this.$dialog.alert({
                title: '正在获取短信验证码，请稍后'
              });
            } else if (!(/^1[0-9]\d{9}$/.test(this.phone))) {
              this.warringInfo = '请输入正确手机号';
            } else {
              dataInfo.init.$dialog.confirm({
                title: '确认接听语音验证码'
              }).then(() => {
                dataInfo.init.codeNum = 2;
                dataInfo.init.isYY = 0;
                dataInfo.init.getCoding();
              });
            };
          },
          getCode: function() { //获取短信验证码
            dataInfo.init.warringInfo = '';
            if (dataInfo.init.codeNum == 1) { //发送短信 - start
              if (!(/^1[0-9]\d{9}$/.test(this.phone))) {
                this.warringInfo = '请输入正确手机号';
              } else {
                this.codeNum == 2;
                dataInfo.init.getCodeInfo = '发送中..';
                dataInfo.init.isYY=1;
                dataInfo.init.getCoding();
              };
            }; //发送短信 - end
          },
          getCoding: function() { //  获取短信code  dataInfo.init.getCoding();
            dataInfo.timestamp = new Date().getTime().toString();
            dataInfo.phone = '';
            dataInfo.init.phone.split('').forEach(function(item, index) {
              index == 0 || index == 2 || index == 4 || index == 6 || index == 8 ? dataInfo.phone += item : '';
            });
            dataInfo.phoneNum = hex_md5(hex_md5(dataInfo.init.phone.toString()) + (dataInfo.phone + dataInfo.timestamp).toString());
            ajaxTemplate(addressCodeUrl + SERVICE.fxCode, function(data) {
              if (data.tag == 0) {
                dataInfo.dataTime = 60;
                dataInfo.times = setInterval(function() {
                  if (dataInfo.dataTime <= 1) {
                    dataInfo.init.getCodeInfo = '重新发送';
                    dataInfo.times = window.clearInterval(dataInfo.times);
                    dataInfo.init.codeNum = 1;
                  } else {
                    dataInfo.init.codeNum = 2;
                    dataInfo.init.getCodeInfo = --dataInfo.dataTime + 's';
                  }
                }, 1000);
              } else {
                dataInfo.init.codeNum = 1;
                dataInfo.init.getCodeInfo = '重新发送';
                dataInfo.init.warringInfo = data.errMsg;
              };
            }, {
              phone: dataInfo.init.phone,
              code: dataInfo.timestamp,
              sign: dataInfo.phoneNum,
              msgType: 'H5FXDLYZM',
              isYY: dataInfo.init.isYY
            });
          },
          wxLogin: function() { //微信授权登录
            if (!!userId && !!openId && !access_token) { //key过期验证
              this.getToken();
            } else if ((!openId && !getQueryString('openid')) || getQueryString('hasUnionid') == 'n') { //第一次授权登录
              location.href = "http://www.fulibailing.top/service/silence/authBySnsapiUserInfo?appid=wx004631167cb38682&redirect_url=" + addressCodeUrl + "api/uc/auth/wx/auth/v2?zmtUrl=" + window.btoa(window.location.href.split('//')[1]) + '&m=h5&v=1.0&pt=fxh5c&flag=44654454545151515132fdsfdf';
            } else if (!openId) { //第一次授权登录回来
              if (!getQueryString('uniqueCode') || getQueryString('uniqueCode') == 'null' || getQueryString('isBindingPhone') == 'y') {
                this.switchNum = 2; //跳转填写手机号弹窗
              } else {
                dataInfo.saveData = new Object();
                dataInfo.saveData.openId = getQueryString('openid');
                dataInfo.saveData.uniqueCode = getQueryString('uniqueCode');
                dataInfo.saveData.access_token = getQueryString('access_token');
                setTimeout(function(){
                  setCookie('zmtH5Mall', escape(JSON.stringify(dataInfo.saveData)), 30);
                  dataInfo.init.detectionJump();
                },300);
              };
            } else if (!userId || !access_token) { //未给用户id或者token 跳转手机号录入
              this.switchNum = 2;
            } else {
              this.detectionJump();
            };
          },
          getToken: function() { //有用户id 直接跳此填写token
            ajaxTemplate(addressCodeUrl + SERVICE.getToken, function(data) {
              if (data.tag == 0) {
                dataInfo.saveData = new Object();
                dataInfo.saveData.openId = openId;
                dataInfo.saveData.uniqueCode = data.data.result.uniqueCode;
                dataInfo.saveData.access_token = data.data.result.access_token;
                setCookie('zmtH5Mall', escape(JSON.stringify(dataInfo.saveData)), 30);
                dataInfo.init.detectionJump();
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId,
            });
          },
          detectionWx: function() {
            if (!!this.isWx && !!getQueryString('openid')) { //回调回来的状态
              this.wxLogin();
            };
          },
          detectionJump: function() { //回调回原页面
            dataInfo.cookieInfo = unescape(getCookie('zmtH5MallUrl')); //解决商品详情跳订单后跳转登录无法返回问题
            dataInfo.cookieInfo.indexOf('"') == 0 ? dataInfo.cookieInfo.substring(1, dataInfo.cookieInfo.length - 1) : JSON.parse(dataInfo.cookieInfo);
            dataInfo.locationInfo = '?' + dataInfo.cookieInfo;
            dataInfo.locationInfo.indexOf('?"') == 0 ? dataInfo.locationInfo = dataInfo.locationInfo.substring(0, dataInfo.locationInfo.length - 1).replace('?"', '?') : '';
            dataInfo.jumpUrl = "../html/" + window.location.href.split('html?zmhUrl=')[1];
            dataInfo.jumpUrl.indexOf('login.html') > 0 ? dataInfo.jumpUrl = '../html/index.html' : '';
            dataInfo.formerUrl = window.location.href.indexOf('&openid=') < 0 ? dataInfo.jumpUrl + dataInfo.locationInfo : dataInfo.jumpUrl.split('&openid=')[0] + dataInfo.locationInfo;
            setTimeout(function() {
              location.href = dataInfo.formerUrl;
            }, 300);
          },
        },
        computed: {
          active: function() { //手机号 code监听
            return !!/^1[0-9]\d{9}$/.test(this.phone) && this.code.length == 6;
          }
        },
      });
      dataInfo.init.detectionWx();
    }
  };
window.location.href.indexOf('login.html?') < 0 ? location.href = window.location.href + '?zmhUrl=index.html' : '';
getQueryString('zmhUrl') === '' ? location.href = window.location.href + 'index.html' : '';
template.init();
